package com.xian.ai.code.core.saver;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.xian.ai.code.ai.model.MultiFileCodeResult;
import com.xian.ai.code.exception.ErrorCode;
import com.xian.ai.code.exception.ThrowUtils;
import com.xian.ai.code.model.enums.CodeGenTypeEnum;

import java.io.File;

/**
 * @ClassName MultiFilesCodeSaveTemplate
 * @Description TODO
 * @Author Zhangxn
 * @Version 1.0
 */
public class MultiFilesCodeSaveTemplate extends CodeFileSaveTemplate<MultiFileCodeResult> {
    @Override
    protected CodeGenTypeEnum getCodeType() {
        return CodeGenTypeEnum.MULTI_FILE;
    }

    @Override
    protected void validateInput(MultiFileCodeResult result) {
        super.validateInput(result);
        ThrowUtils.throwIf(StrUtil.isBlank(result.getHtmlCode()), ErrorCode.PARAMS_ERROR,"Html文件不能为空");
        ThrowUtils.throwIf(StrUtil.isBlank(result.getJsCode()), ErrorCode.PARAMS_ERROR,"js文件不能为空");
        ThrowUtils.throwIf(StrUtil.isBlank(result.getCssCode()), ErrorCode.PARAMS_ERROR,"css文件不能为空");
    }

    @Override
    protected void saveFiles(MultiFileCodeResult result, String baseDirPath) {
        writeToFile(baseDirPath, "index.html", result.getHtmlCode());
        writeToFile(baseDirPath, "style.css", result.getCssCode());
        writeToFile(baseDirPath, "script.js", result.getJsCode());
    }
}
